package l1j.server.server.model;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.Timestamp;//TODO 記錄文件檔 by 阿傑



import l1j.opqlo.NewSystem.各項記錄輸出;
import l1j.opqlo.Server.L1World.L1World;
import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.CharObjidTable;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_TradeAddItem;
import l1j.server.server.serverpackets.S_TradeStatus;

// Referenced classes of package l1j.server.server.model:
// L1Trade

public class L1Trade {

	private static final Logger _log = Logger.getLogger(L1Trade.class.getName());

	public void TradeAddItem(final L1PcInstance player, final L1ItemInstance l1iteminstance, long itemcount) {
		final L1PcInstance trading_partner = (L1PcInstance) L1World.getInstance().findObject(player.getTradeID());
		//opqlo 防洗
		final long itemCount = l1iteminstance.getCount();
		itemcount = Math.abs(itemcount);
		itemcount = (int) Math.min(itemcount,itemCount);
		//opqlo 防洗
		if (l1iteminstance != null && trading_partner != null) {
			if (!l1iteminstance.isEquipped()) {

				//opqlo 防洗
				if(itemcount > 0 && itemcount < 2000000000
						&& itemCount > 0 && itemCount >= itemcount)
				{
					//opqlo 防洗

					if (itemCount < itemcount || 0 > itemcount) {
						player.sendPackets(new S_TradeStatus(1));
						trading_partner.sendPackets(new S_TradeStatus(1));
						player.setTradeOk(false);
						trading_partner.setTradeOk(false);
						player.setTradeID(0);
						trading_partner.setTradeID(0);
						return;
					}
					player.getInventory().tradeItem(l1iteminstance, itemcount,L1World.getInstance().getTradeInventory(player.getId()));
					player.sendPackets(new S_TradeAddItem(l1iteminstance,itemcount, 0));
					trading_partner.sendPackets(new S_TradeAddItem(l1iteminstance,itemcount, 1));
				}
			}
		}
	}

	public void TradeCancel(final L1PcInstance player) {
		try{

			final L1TradeInventory player_inv = L1World.getInstance().getTradeInventory(player.getId());
			if(player_inv == null){
				return;
			}
			player_inv.TradeOk(player.getInventory());
			player.sendPackets(new S_TradeStatus(1));
			player.setTradeOk(false);
			player.setTradeID(0);
			final int partnerId = player_inv.getPartnerId();
			if(partnerId !=0){
				final L1PcInstance trading_partner = (L1PcInstance) L1World.getInstance().findObject(partnerId);
				if(trading_partner !=null){
					final L1TradeInventory partner_inv = L1World.getInstance().getTradeInventory(partnerId);
					if(partner_inv == null){
						return;
					}
					//			if(partner_inv.getPartnerId() == player.getId()){
					partner_inv.TradeOk(trading_partner.getInventory());
					partner_inv.setPartnerId(0);
					trading_partner.sendPackets(new S_TradeStatus(1));
					trading_partner.setTradeOk(false);
					trading_partner.setTradeID(0);
					//			}
				}
			}
			player_inv.setPartnerId(0);
		}catch(final Exception e){
			_log.log(Level.SEVERE , e.getLocalizedMessage() , e);
		}
		/*
		int cnt;
		 final L1PcInstance trading_partner = (L1PcInstance) L1World.getInstance().findObject(player.getTradeID());
			final L1TradeInventory player_inv = L1World.getInstance().getTradeInventory(player.getId());
		if (trading_partner != null) {
			 final List<L1ItemInstance> player_tradelist = player_inv.getItems();
			 final int player_tradecount = player_inv.getSize();

			for (cnt = 0; cnt < player_tradecount; cnt++) {
				 final L1ItemInstance l1iteminstance1 = player_tradelist.get(0);
				 player_inv.tradeItem(l1iteminstance1,l1iteminstance1.getCount(), player.getInventory());
			}
			final L1TradeInventory trading_partner_inv = L1World.getInstance().getTradeInventory(trading_partner.getId());
			 final List<L1ItemInstance> trading_partner_tradelist = trading_partner_inv.getItems();
			 final int trading_partner_tradecount = trading_partner_inv.getSize();

			for (cnt = 0; cnt < trading_partner_tradecount; cnt++) {
				 final L1ItemInstance l1iteminstance2 = trading_partner_tradelist.get(0);
				 trading_partner_inv.tradeItem(l1iteminstance2, l1iteminstance2.getCount(),trading_partner.getInventory());
			}
		 */
		//			player.sendPackets(new S_TradeStatus(1));
		//			trading_partner.sendPackets(new S_TradeStatus(1));
		//			player.setTradeOk(false);
		//			trading_partner.setTradeOk(false);
		//			player.setTradeID(0);
		//			trading_partner.setTradeID(0);
		//		}

	}


	public void TradeOK(final ClientThread client ,final L1PcInstance player) {
		int cnt;
		final int player_Id = player.getId();
		final L1TradeInventory player_inv = L1World.getInstance().getTradeInventory(player_Id); // 取得自身交易背包
		final int partner_Id = player_inv.getPartnerId();
		final L1PcInstance trading_partner = (L1PcInstance) L1World.getInstance().findObject(partner_Id);
		final String playerName = player.getName();
		final String partnerName ;
		String partnerIp ;
		if (trading_partner != null) {
			partnerName = CharObjidTable.get().isChar(partner_Id);
			try{
				partnerIp = trading_partner.getNetConnection().getIp().toString();
			}catch(final Exception e){
				partnerIp = "空";
			}
		}else{
			partnerName = "";
			partnerIp = "空";
		}
		String playerIp ;
		try{
			playerIp = client.getIp().toString();
		}catch(final Exception e){
			playerIp = "空";
		}


		if(partner_Id !=0) {

			final L1TradeInventory trading_partner_inv = L1World.getInstance().getTradeInventory(partner_Id);// 取得對方背包

			final L1Inventory playerInputInv = player_inv.getInputInventory(); //取得自身交易接收背包
			final L1Inventory partnerInputInv = trading_partner_inv.getInputInventory(); //取得對方交易接收背包

			final List<L1ItemInstance> player_tradelist = player_inv.getItems();//取得自身背包所有物品
			final int player_tradecount = player_inv.getSize();//取得自身背包物品數量

			final List<L1ItemInstance> trading_partner_tradelist = trading_partner_inv.getItems();//取得對方背包所有物品
			final int trading_partner_tradecount = trading_partner_inv.getSize();//取得對方背包所有物品


			final StringBuilder out = new StringBuilder();

			out.append("時間:	").append(new Timestamp(System.currentTimeMillis())).append("\r\n\r\n")
			.append(playerIp).append( "	邀請方:【").append(playerName).append("】\r\n")
			.append(partnerIp).append( "	接受方:【").append(partnerName).append("】\r\n\r\n")

			.append("----------【").append(partnerName).append("】獲得物品清單如下:----------\r\n\r\n");


			for (cnt = 0; cnt < player_tradecount; cnt++) {
				boolean reIdentified = false;
				final L1ItemInstance l1iteminstance1 = player_tradelist.get(0);

				player_inv.tradeItem(l1iteminstance1,l1iteminstance1.getCount(),partnerInputInv);//交易物品==自身背包 -> 對方交易接收背包

				if(l1iteminstance1.getItem().getType2()!=0 && !l1iteminstance1.isIdentified()){
					l1iteminstance1.setIdentified(true);
					reIdentified = true;
				}
				out.append("[").append(l1iteminstance1.getItemId()).append("]		").append(l1iteminstance1.getViewName()).append("		流水號:【  ").append(l1iteminstance1.getId()).append(" 】\r\n");
				if(reIdentified){
					l1iteminstance1.setIdentified(false);
					reIdentified=false;
				}
			}

			out.append("\r\n\r\n----------【").append( playerName).append("】獲得物品清單如下:----------\r\n\r\n");

			for (cnt = 0; cnt < trading_partner_tradecount; cnt++) {
				boolean reIdentified = false;
				final L1ItemInstance l1iteminstance2 = trading_partner_tradelist.get(0);

				trading_partner_inv.tradeItem(l1iteminstance2, l1iteminstance2.getCount(),playerInputInv);//交易物品==對方背包 -> 自身交易接收背包

				if(l1iteminstance2.getItem().getType2()!=0 && !l1iteminstance2.isIdentified()){
					l1iteminstance2.setIdentified(true);
					reIdentified = true;
				}
				out.append("[").append(l1iteminstance2.getItemId()).append("]		").append(l1iteminstance2.getViewName()).append("		流水號:【  ").append(l1iteminstance2.getId()).append(" 】\r\n");
				if(reIdentified){
					l1iteminstance2.setIdentified(false);
					reIdentified=false;
				}
			}
			try{
				trading_partner_inv.TradeOk();
				if (trading_partner != null) {
					trading_partner_inv.TradeOk(trading_partner.getInventory());
				}
			}catch(final Exception e){
				_log.log(Level.SEVERE, e.getLocalizedMessage() , e);
			}
			trading_partner_inv.setPartnerId(0);

			try{
				player_inv.TradeOk();
				player_inv.TradeOk(player.getInventory());
			}catch(final Exception e){
				_log.log(Level.SEVERE, e.getLocalizedMessage() , e);
			}
			player_inv.setPartnerId(0);

			player.sendPackets(new S_TradeStatus(0));
			trading_partner.sendPackets(new S_TradeStatus(0));
			player.setTradeOk(false);
			trading_partner.setTradeOk(false);
			player.setTradeID(0);
			trading_partner.setTradeID(0);

			//opqlo 交易記錄
			各項記錄輸出.LogSave(9,out.toString());
			if(player.isMonitor() ||trading_partner.isMonitor()){
				各項記錄輸出.LogSave(8,out.toString());
			}
			out.delete(0, out.length());
		}
	}
}
